#include "cexeccmd.h"
#include <QDebug>
#include <QFile>
#include <QTime>

#define PROCESS_INTERVAL    200

CExecCmd::CExecCmd(QObject *parent) :
    QObject(parent)
{
}

bool CExecCmd::doCmd(QString cmd,QStringList argv)
{
   qDebug() << "args:" << cmd << " " << argv;

   _pro.start(cmd,argv);

   if(_pro.waitForFinished(12000))
   {
       int ec = _pro.exitCode();
       if(ec!=0)
       {
          qDebug() << "CExecCmd:" << _pro.readAllStandardError();
          return false;
       }

       return true;
   }

   return false;
}

bool CExecCmd::textChange(QString &context,QString tagStart,QString tagEnd,QString replace)
{
    int start = context.indexOf(tagStart);
    int end = context.indexOf(tagEnd);
    if(start>0 && end > 0)
    {
        context.replace(start,end-start,tagStart+replace);
        return true;
    }

    qDebug() << "textChange: fail " << start << " " << end;

    return false;
}

QString CExecCmd::get_text(QString context,QString tagStart,QString tagEnd)
{
    int start = context.indexOf(tagStart);
    int end = context.indexOf(tagEnd);
    if(start>0 && end > 0)
    {
        return context.mid(start,end-start);
    }

    return "";
}

char* CExecCmd::get_network_configure(int index)
{

}

int CExecCmd::set_network_configure(QString ip, QString mask,
                                    QString gw, QString dns, int sip)
{
    if(ip.isEmpty() || ip.size()<8)
        return -2;

    int ret = 0;

    QFile ipfile("/data/TTG_NETWORK/set_ip.sh");
    if(ipfile.open(QFile::ReadWrite))
    {
        ipfile.resize(0);
        ipfile.write("#!/system/bin/sh\n");
        if(sip==1)
        {
            ipfile.write("sleep 20\n");
            ipfile.write("/data/TTG_NETWORK/start_sip.sh\n");
        }
        ipfile.close();
    }
    else
    {
        qDebug() << "SET_IP open fail!";
        return -1;
    }

    if(sip == 1)
    {
        ret = set_sipserver_ip(ip);
        qDebug() << "set ip for sip " << ret;
    }

    return ret;
}

int CExecCmd::set_sipserver_ip(QString ip)
{
    if(ip.isEmpty() || ip.size() < 4)
        return -2;

    QByteArray bak;
    QFile sfile("/data/TTG_NETWORK/opensip/etc/opensips/opensips.cfg.bak");
    if(sfile.open(QFile::ReadOnly))
    {
        bak = sfile.readAll();
        sfile.close();
    }
    else
    {
        qDebug() << "SET_SIP bak fail!";
        return -1;
    }

    sfile.setFileName("/data/TTG_NETWORK/opensip/etc/opensips/opensips.cfg");
    if(sfile.open(QFile::WriteOnly))
    {
        sfile.resize(0);
        sfile.write(bak);
        sfile.write("\n");
        QString addip = "listen=udp:"+ip+":5060\n";
        sfile.write(addip.toLocal8Bit());
        sfile.close();
    }
    else
    {
        qDebug() << "SET_SIP cfg fail!";
        return -2;
    }

   qDebug() << "set sip server :" << ip ;

   return 0;
}

int CExecCmd::doExec()
{
    QString cmd = "su";
    QStringList argv;
    argv << "root";
    qDebug() << "Exec: su " << doCmd(cmd,argv) << "\n";

    argv.clear();
    cmd = "/data/TTG_NETWORK/set_ip.sh";
    qDebug() << "Exec: ip " << doCmd(cmd,argv) << "\n";

    return 0;
}
